﻿// Package class definition
var DraggablePeca = Class.create();
DraggablePeca.prototype = {
	initialize: function () {
		this.tagId = "";  // HTML id for this item
		this.itemType = -1; // 1=package, 2=event, 3=combo, 4=asset
		this.timeSlot = [];  // the index of the time slot for this item in the list
		this.listType = -1;  // listType this item belongs to 0 = slotted and 1 = timeline
		this.dbId = -1;  // DB id for this item
		this.name = "";  // descriptive name
		this.duration = [];  // duration array
		this.height = -1; // calculated height
		this.systemId = -1;  // the remainder are all DB values
		this.zoneId = -1;
		this.channelsNN = "";
		this.isCombo = false;
		this.textTemplateId = -1;
		this.thisEnabled = false;
		this.thisDayPart = -1;
		this.thisStartDate = [];
		this.thisEndDate = [];
		this.thisColor = "";
		this.enabledNN = false;
		this.dayPartNN = -1;
		this.startDateNN = [];
		this.endDateNN = [];
		this.ingestedNN = false;
		this.onPlayersNN = false;
		this.colorNN = "";
		this.htmlNN = "";
		this.percentBreakdownNN = "";	
		this.cueStringStart = "";
		this.cueStringEnd = "";
		this.openForEditUserId = -1;
		this.openForEditDateTime = [];
		this.isWebControlPackage = false;
		this.fontColor = "";
	},

	// function to destroy all properties, stop all event observers, and destroy object
	destroy: function () {},
	
	// function to load all properties from DB
	load: function (itemType, timeSlot, listType, dbId, name, duration, systemId, zoneId, channelsNN, 
	                isCombo, textTemplateId, thisEnabled, thisDayPart, thisStartDate, thisEndDate, thisColor,
	                enabledNN, dayPartNN, startDateNN, endDateNN, ingestedNN, onPlayersNN, colorNN, htmlNN, 
	                percentBreakdownNN, cueStringStart, cueStringEnd, openForEditUserId, openForEditDateTime,
	                isWebControlPackage) {
		// initialize properties
		if (activeEditor) {
		    this.tagId = "peca_" + activeEditor.draggablePecaIndex++;
		} else {
		    this.tagId = "pagePeca_" + itemType;
		}
		this.itemType = itemType;
		this.timeSlot = timeSlot;
		this.listType = listType;
		this.dbId = dbId;
		this.name = name;
		this.duration = duration;
		this.systemId = systemId;
		this.zoneId = zoneId;
		this.channelsNN = channelsNN;
		this.isCombo = isCombo;
		this.textTemplateId = textTemplateId;
		this.thisEnabled = thisEnabled;
		this.thisDayPart = thisDayPart;
		this.thisStartDate = thisStartDate;
		this.thisEndDate = thisEndDate;
		this.thisColor = "#" + thisColor;
		this.enabledNN = enabledNN;
		this.dayPartNN = dayPartNN;
		this.startDateNN = startDateNN;
		this.endDateNN = endDateNN;
		this.ingestedNN = ingestedNN;
		this.onPlayersNN = onPlayersNN;
		this.colorNN = colorNN;
		this.htmlNN = htmlNN;
		this.percentBreakdownNN = percentBreakdownNN;
		this.cueStringStart = cueStringStart;
		this.cueStringEnd = cueStringEnd;
		this.openForEditUserId = openForEditUserId;
		this.openForEditDateTime = openForEditDateTime;
	    this.isWebControlPackage = isWebControlPackage;
	    
	    // set font color based on background color
	    this.fontColor = this.assignFontColor();
		
		// calculate height based on duration
		if (listType == 1 && activeEditor) {  // timeline list
		    var durationInSecs = parseInt((this.duration[3] * 3600),10) + parseInt((this.duration[4] * 60),10) + parseInt((this.duration[5]),10);
			//if (durationInSecs % activeEditor.interval != 0) alert ('Duration on db item ' + dbId + ' does not match editor intervals');
			this.height = Math.ceil(activeEditor.pixelsToSecs * durationInSecs);
			this.height += 1;  // added due to margin-top = -1
		}
	},
	
	// function to assign font color based on this.color property
	assignFontColor: function() {
	    if (this.thisColor == "") {
	        return "#000000";
	    }
        
        if (this.thisColor.length == 6) {
            var bgHexR = this.thisColor.charAt(0);
            var bgHexG = this.thisColor.charAt(2);
            var bgHexB = this.thisColor.charAt(4);
            var hexMatch = /[0-5]/;
            var numMatches = 0;
            if (bgHexR.match(hexMatch)) numMatches++;
            if (bgHexG.match(hexMatch)) numMatches++;
            if (bgHexB.match(hexMatch)) numMatches++;
            return (numMatches > 0) ? "#ffffff" : "#000000";
        } else if (this.thisColor.length == 7) {
            var bgHexR = this.thisColor.charAt(1);
            var bgHexG = this.thisColor.charAt(3);
            var bgHexB = this.thisColor.charAt(5);
            var hexMatch = /[0-5]/;
            var numMatches = 0;
            if (bgHexR.match(hexMatch)) numMatches++;
            if (bgHexG.match(hexMatch)) numMatches++;
            if (bgHexB.match(hexMatch)) numMatches++;
            return (numMatches > 0) ? "#ffffff" : "#000000";
        } else {
    	    return "#000000";
    	}
	},
	
	// function to render the package and attach to list (only to be called once)
	render: function() {
		if (this.listType == 0) {
			var dragItem = document.createElement ('div');
			dragItem.className = "dragCell";
			dragItem.id = this.tagId;
			dragItem.style.width = activeEditor.cellWidth + "px";
			dragItem.style.padding = "0 " + activeEditor.cellPadding + "px";
			dragItem.style.height = activeEditor.cellHeight + "px";
			dragItem.innerHTML = this.name; // + " " + this.tagId;
			dragItem.style.backgroundColor = this.thisColor; // '#3399ff';
			dragItem.style.color = this.fontColor;
			return dragItem;
		} else {
			var dragItem = document.createElement ('li');
			dragItem.className = "dragLi";
			dragItem.id = this.tagId;
			dragItem.style.width = activeEditor.cellWidth + "px";
			dragItem.style.padding = "0 " + activeEditor.cellPadding + "px";
			dragItem.style.height = (this.height-2) + "px";  // -2 for overlapping border
			if (this.height <= 13) dragItem.style.lineHeight = '';
			dragItem.innerHTML = this.name; // + " " + this.tagId;
			var nameArray = this.name.split('_');
			dragItem.style.backgroundColor = this.thisColor;
			dragItem.style.color = this.fontColor;
			return dragItem;
		}
	},
	
	// function to copy package and return the copy
	copy: function () {
		var newPeca = new DraggablePeca();
	
		newPeca.tagId = "peca_" + activeEditor.draggablePecaIndex++;
		newPeca.itemType = this.itemType;
		newPeca.timeSlot = this.timeSlot;
		newPeca.listType = this.listType;
		newPeca.dbId = this.dbId;
		newPeca.name = this.name;
		newPeca.duration = this.duration;
		newPeca.height = this.height;
		newPeca.systemId = this.systemId;
		newPeca.zoneId = this.zoneId;
		newPeca.channelsNN = this.channelsNN;
		newPeca.isCombo = this.isCombo;
		newPeca.textTemplateId = this.textTemplateId;
		newPeca.thisEnabled = this.thisEnabled;
		newPeca.thisDayPart = this.thisDayPart;
		newPeca.thisStartDate = this.thisStartDate;
		newPeca.thisEndDate = this.thisEndDate;
		newPeca.thisColor = this.thisColor;
		newPeca.enabledNN = this.enabledNN;
		newPeca.dayPartNN = this.dayPartNN;
		newPeca.startDateNN = this.startDateNN;
		newPeca.endDateNN = this.endDateNN;
		newPeca.ingestedNN = this.ingestedNN;
		newPeca.onPlayersNN = this.onPlayersNN;
		newPeca.colorNN = this.colorNN;
		newPeca.htmlNN = this.htmlNN;
		newPeca.percentBreakdownNN = this.percentBreakdownNN;
		newPeca.cueStringStart = this.cueStringStart;
		newPeca.cueStringEnd = this.cueStringEnd;
		newPeca.openForEditUserId = this.openForEditUserId;
		newPeca.openForEditDateTime = this.openForEditDateTime;
	    newPeca.isWebControlPackage = this.isWebControlPackage;		
        newPeca.fontColor = this.fontColor;
        
		return newPeca;
	},
	
	// function to overwrite a peca with calling peca's properties
	overwrite: function (oldPeca, dragEl) {
		// tagId is unchanged
		//change color and name of drag element
		dragEl.innerHTML = this.name;
		dragEl.style.backgroundColor = this.thisColor;
		dragEl.style.color = this.fontColor;
		
		oldPeca.itemType = this.itemType;
		oldPeca.timeSlot = this.timeSlot;
		oldPeca.listType = this.listType;
		oldPeca.dbId = this.dbId;
		oldPeca.name = this.name;
		oldPeca.duration = this.duration;
		oldPeca.height = this.height;
		oldPeca.systemId = this.systemId;
		oldPeca.zoneId = this.zoneId;
		oldPeca.channelsNN = this.channelsNN;
		oldPeca.isCombo = this.isCombo;
		oldPeca.textTemplateId = this.textTemplateId;
		oldPeca.thisEnabled = this.thisEnabled;
		oldPeca.thisDayPart = this.thisDayPart;
		oldPeca.thisStartDate = this.thisStartDate;
		oldPeca.thisEndDate = this.thisEndDate;
		oldPeca.thisColor = this.thisColor;
		oldPeca.enabledNN = this.enabledNN;
		oldPeca.dayPartNN = this.dayPartNN;
		oldPeca.startDateNN = this.startDateNN;
		oldPeca.endDateNN = this.endDateNN;
		oldPeca.ingestedNN = this.ingestedNN;
		oldPeca.onPlayersNN = this.onPlayersNN;
		oldPeca.colorNN = this.colorNN;
		oldPeca.htmlNN = this.htmlNN;
		oldPeca.percentBreakdownNN = this.percentBreakdownNN;
		oldPeca.cueStringStart = this.cueStringStart;
		oldPeca.cueStringEnd = this.cueStringEnd;
		oldPeca.openForEditUserId = this.openForEditUserId;
		oldPeca.openForEditDateTime = this.openForEditDateTime;
	    oldPeca.isWebControlPackage = this.isWebControlPackage;
	    oldPeca.fontColor = this.fontColor;
	},
		
	// function to change the color of a package
	changeColor: function () {},
	
}



